﻿using HarmonyLib;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace SuperMe
{
    class SuperMe_StatFactor
    {
        public static void CreateFactor()
        {
            var scenario = Find.Scenario;
            var Traverse_Field_Inst = Traverse.Create(scenario).Field("parts");
            if (Traverse_Field_Inst.FieldExists())
            {
                List<ScenPart> parts = Traverse_Field_Inst.GetValue<List<ScenPart>>();
                foreach (ModifyInfo mi in SuperMeMod.settings.default_stat_defs)
                {
                    CreatFactor(parts, mi.stat_def, mi.factor);
                }
            }
            else
            {
                SuperMeDef.Log("Can not find Fields {parts } in Find.Scenario");
            }
        }
        private static void CreatFactor(List<ScenPart> parts, RimWorld.StatDef stat_def, float factor)
        {
            if (HasExistStatDefAndModify(stat_def, factor))
                return;

            ScenPartDef spd = SuperMe_DataMgr.FindStatFactor();
            ScenPart sp = ScenarioMaker.MakeScenPart(spd);
            ScenPart_StatFactor sp_sf = sp as ScenPart_StatFactor;
            if (sp_sf != null)
            {
                Set_ScenPart_StatFactor_Field(sp_sf, stat_def, factor);
                parts.Add(sp);
                SuperMeDef.Log("Creat " + stat_def.GetType().ToString() + " Succed.");
            }
        }
        private static bool HasExistStatDefAndModify(RimWorld.StatDef stat_part_def, float factor)
        {
            foreach (ScenPart sp in SuperMe_DataMgr.has_def_scen_parts)
            {
                ScenPart_StatFactor scenPart_StatFactor = sp as ScenPart_StatFactor;
                if (scenPart_StatFactor != null)
                {
                    if (Compare_And_Modify_ScenPart_StatFactor_Field(scenPart_StatFactor, stat_part_def, factor))
                    {
                        return true;
                    }
                }
            }
            SuperMeDef.Log("Can not find ScenPart_StatFactor in SuperMe_DataMgr.has_def_scen_parts");
            return false;
        }
        private static void Set_ScenPart_StatFactor_Field(ScenPart_StatFactor sp_sf_inst, RimWorld.StatDef stat_def, float factor)
        {
            var Traverse_ScenPart_StatFactor_Inst = Traverse.Create(sp_sf_inst);
            var Stat_Field = Traverse_ScenPart_StatFactor_Inst.Field("stat");
            var Factor_Field = Traverse_ScenPart_StatFactor_Inst.Field("factor");
            if (Stat_Field.FieldExists() && Factor_Field.FieldExists())
            {
                Stat_Field.SetValue(stat_def);
                Factor_Field.SetValue(factor);
            }
            else
            {
                SuperMeDef.Log("Can not find Fields { stat , factor } in ScenPart_StatFactor");
            }
        }
        private static bool Compare_And_Modify_ScenPart_StatFactor_Field(ScenPart_StatFactor sp_sf_inst, RimWorld.StatDef stat_def, float factor)
        {
            var Traverse_ScenPart_StatFactor_Inst = Traverse.Create(sp_sf_inst);
            var Stat_Field = Traverse_ScenPart_StatFactor_Inst.Field("stat");
            var Factor_Field = Traverse_ScenPart_StatFactor_Inst.Field("factor");
            if (Stat_Field.FieldExists() && Factor_Field.FieldExists())
            {
                RimWorld.StatDef stat_def_com = Stat_Field.GetValue() as RimWorld.StatDef;
                if (stat_def_com == stat_def)
                {
                    SuperMeDef.Log("Can Fields { stat , factor } in ScenPart_StatFactor stat_def_com:" + stat_def_com.defName + " stat_def:" + stat_def.defName);
                    Factor_Field.SetValue(factor);
                    return true;
                }
            }
            else
            {
                SuperMeDef.Log("Can not find Fields { stat , factor } in ScenPart_StatFactor");
            }
            return false;
        }
    }
}
